package com.xm.sort_algorithm;

import java.util.Arrays;

/**
 * Author：xm
 * Project：interview
 * Date：2025/8/14  0:22
 */

public class _02_QuickSort2 {
    public static void main(String[] args) {
        // int[] arr = {5, 1, 15, 30, 3, 15, 16};
        // int[] arr = {20, 2, 7, 12, 15, 1, 6, 8};
        int[] arr = {50, 50, 70, 90, 80, 10, 20, 60};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));

    }

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivotIndex = partition(arr, low, high);
            quickSort(arr, low, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, high);
        }
    }

    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= pivot) {
                high--;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] <= pivot) {
                low++;
            }
            arr[high] = arr[low];
        }
        arr[low] = pivot;
        return low;
    }
}
